/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.gui;

import com.pehulja.mmdo.coursework.math.Equation;
import com.pehulja.mmdo.coursework.math.GoalFunction;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author pehulja
 */
public class BusinessRestrictionsTableModel extends AbstractTableModel{
    private static final int defaultColumnCount = 3;
    private Vector<Vector<Object>> data;
    private Vector <String> columnIdentifiers;    
    public BusinessRestrictionsTableModel() {
       this(defaultColumnCount, 1);
    }
    
    public BusinessRestrictionsTableModel(int varNumber, int restNumber) {
       columnIdentifiers = new Vector<String>();
        for(int i = 0; i < varNumber; i++)
            columnIdentifiers.add("x"+(i+1));
        columnIdentifiers.add("Inequantity");
        columnIdentifiers.add("b");
        data = new Vector<Vector<Object>>();
        for(int i = 0; i<restNumber; i++)
            appendEmptyDataRow();
    }

    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return columnIdentifiers.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.get(rowIndex).get(columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return (String) columnIdentifiers.get(column);
    }
    
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true; //To change body of generated methods, choose Tools | Templates.
    }

    public void setValueAt(Object value, int row, int col) {
        data.get(row).set(col, value);
        fireTableCellUpdated(row, col);
    }

    public void addColumnToEnd() {
        columnIdentifiers.insertElementAt("x"+(getColumnCount()-1), getColumnCount()-2); 
	for(Vector i : data)
            i.insertElementAt("", i.size()-2);
        fireTableStructureChanged();
    }
    
    public void removePreLastColumn() throws Exception{
        if(getColumnCount()>=2){
            columnIdentifiers.remove(getColumnCount()-3); 
            for(Vector i : data)
                i.remove(getColumnCount()-2);
            fireTableStructureChanged();
        }else{
            throw new Exception("Невозможно удалить столбец");
        }
    }
    
    public void removeLastDataRow(){
        if(data.size()>0)
            data.remove(data.size()-1);
        fireTableRowsDeleted(data.size()-1, data.size()-1);
    }
    
    public void appendEmptyDataRow(){
        data.add(new Vector<Object>());
        for(int i=0; i < getColumnCount(); i++){
            if(i==getColumnCount()-2)
                data.get(data.size()-1).add(Equation.InequalitySight.BIGGERorEQ);
            else
                data.get(data.size()-1).add("");
        }
         fireTableRowsInserted(data.size()-1, data.size()-1);
    }
    
    
    
    public void setData(Vector<Vector<Object>> data){
        columnIdentifiers = new Vector<String>();
        for(int i = 0; i < data.get(0).size()-2; i++)
            columnIdentifiers.add("x"+(i+1));
        columnIdentifiers.add("Inequantity");
        columnIdentifiers.add("b");
        this.data = data;
    }
}
